package com.fire.web.modules.all.controller;

import com.fire.web.modules.all.entity.AjaxResult;
import com.fire.web.modules.all.service.DeviceService;
import com.fire.web.modules.all.validation.*;
import com.smartthing.spi.SmartThingException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author yangxiaotian
 * @version 1.0
 * @date 2019/2/23 11:27
 * @description:
 */
@Controller
public class DeviceController {
    private final static Logger blogin = Logger.getLogger(DeviceController.class);
    @Autowired
    private DeviceService deviceService;

    /**
     * 查询设备（传感器）列表
     *
     * @return
     * @author yangxiaotian
     */
    @PostMapping(value = "/getdeviceslist")
    @ResponseBody
    public AjaxResult getdevices(@Validated SiteIdDTO siteIdDTO, BindingResult bindingResult,HttpServletRequest request, HttpServletResponse response) throws SmartThingException {
        AjaxResult result = null;
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                System.out.println(fieldError.getDefaultMessage());
            }
        }
        result = deviceService.queryDevicesList(siteIdDTO,request,response);
        return result;
    }


    /**
     * 查询设备（传感器）详情
     *
     * @return
     * @author yangxiaotian
     */
    @PostMapping(value = "/getdevicedetail")
    @ResponseBody
    public AjaxResult getdevicedetail(@Validated DeviceIdDTO deviceIdDTO, BindingResult bindingResult) throws SmartThingException {
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                System.out.println(fieldError.getDefaultMessage());
            }
        }

        AjaxResult result = deviceService.queryDeviceDetial(deviceIdDTO);
        return result;
    }

    /**
     * 增加设备（传感器）接口
     *
     * @param
     * @return
     * @author raoyuxing
     */
    @PostMapping(value = "/adddevice")
    @ResponseBody
    public AjaxResult adddevice(@Validated AddDeviceReqDTO addDeviceReqDTO, BindingResult bindingResult) throws SmartThingException {
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                System.out.println(fieldError.getDefaultMessage());
            }
        }
        AjaxResult result = deviceService.creatDevices(addDeviceReqDTO);

        return result;
    }

    /**
     * 创建设备关联
     *
     * @param
     * @return
     * @author yangxiaotian
     */
    @PostMapping(value = "/assign")
    @ResponseBody
    public AjaxResult diveAssignments(@Validated CreatDeviceAssignmentsDTO creatDeviceAssignmentsDTO, BindingResult bindingResult,HttpServletRequest request, HttpServletResponse response) throws SmartThingException {
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                System.out.println(fieldError.getDefaultMessage());
            }
        }
        AjaxResult result = deviceService.CreatDeviceAssignments(creatDeviceAssignmentsDTO,request,response);

        return result;
    }
    /**
     * @author yangxiaotian
     * @date ${DATE} ${TIME}
     * @description:删除设备
     *
     */
    @PostMapping(value = "/deletedevice")
    @ResponseBody
    public AjaxResult deleteDevice(@Validated DeleteDeviceReqDTO deleteDeviceReqDTO,BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                System.out.println(fieldError.getDefaultMessage());
            }
        }
        AjaxResult result = deviceService.deleteDevice(deleteDeviceReqDTO);

        return result;
    }
    /**
     * @author yangxiaotian
     * @date ${DATE} ${TIME}
     * @description:更新现有都设备
     *
     */
    @PostMapping(value = "/updatedevice")
    @ResponseBody
    public AjaxResult updateDevice(@Validated UpdateDeviceRepDTO updateDeviceRepDTO,BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                System.out.println(fieldError.getDefaultMessage());
            }
        }
        AjaxResult result = deviceService.updateDeevice(updateDeviceRepDTO);

        return result;
    }

    /**
     * @author yangxiaotian
     * @date ${DATE} ${TIME}
     * @description:获取设备分类
     *
     */
    @PostMapping(value = "/getdevicecategory")
    @ResponseBody
    public AjaxResult getDeviceCategory(@Validated UpdateDeviceRepDTO updateDeviceRepDTO,BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                System.out.println(fieldError.getDefaultMessage());
            }
        }
        AjaxResult result = deviceService.getDeviceCategory();

        return result;
    }
    /**
     * @author yangxiaotian
     * @date ${DATE} ${TIME}
     * @description:获取设备分类
     *
     */
    @PostMapping(value = "/deletedeviceassign")
    @ResponseBody
    public AjaxResult deleteDeviceAssign(@Validated DeleteDeviceAssignDTO deleteDeviceAssignDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                System.out.println(fieldError.getDefaultMessage());
            }
        }
        AjaxResult result = deviceService.deleteDeviceAssignMents(deleteDeviceAssignDTO);

        return result;
    }
    /**
     * @author yangxiaotian
     * @date ${DATE} ${TIME}
     * @description:取消设备关联
     *
     */
    @PostMapping(value = "/canceldeviceassign")
    @ResponseBody
    public AjaxResult cancelDeviceAssign(@Validated DeleteDeviceAssignDTO deleteDeviceAssignDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                System.out.println(fieldError.getDefaultMessage());
            }
        }
        AjaxResult result = deviceService.cancelDeviceAssignMents(deleteDeviceAssignDTO);

        return result;
    }
}


